package com.longbo.demostudy.common.utils;

import lombok.Data;
import org.springframework.http.HttpStatus;
import java.io.Serializable;

/**
 * JSON响应类
 *
 * @author 龙博
 * @date 2021/3/12 11:35
 */
@Data
public class ApiResponse implements Serializable {

    // 错误码
    private Integer code = HttpStatus.OK.value();

    // 提示语
    private String msg = "操作成功";

    // 返回对象
    private Object data;

    public Integer getCode() {
        return this.code;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(final Object data) {
        this.data = data;
    }

    /**
     * 构造函数
     */
    public ApiResponse() {
    }

    public ApiResponse(String msg) {
        this.msg = msg;
    }

    public ApiResponse(Object data) {
        this.data = data;
    }

    public ApiResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResponse(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ApiResponse success() {
        return new ApiResponse();
    }

    public static ApiResponse success(String msg) {
        return new ApiResponse(msg);
    }

    public ApiResponse success(Object data) {
        return new ApiResponse(HttpStatus.OK.value(), msg, data);
    }

    public static ApiResponse success(String msg, Object data) {
        return new ApiResponse(HttpStatus.OK.value(), msg, data);
    }

    public static ApiResponse error() {
        return new ApiResponse(-1, "操作失败");
    }

    public static ApiResponse error(String msg) {
        return new ApiResponse(-1, msg);
    }

    public static ApiResponse error(Integer code, String msg) {
        return new ApiResponse(code, msg);
    }

    public static ApiResponse error(Integer code, String msg, Object data) {
        return new ApiResponse(code, msg, data);
    }

    public static ApiResponse error(ErrorCode errorCode) {
        return new ApiResponse(errorCode.getCode(), errorCode.getMsg());
    }

    public static ApiResponse error(HttpStatus httpStatus, String msg, Object data) {
        return new ApiResponse(httpStatus.value(), msg, data);
    }

    public Object error(HttpStatus httpStatus, String msg) {
        this.code = httpStatus.value();
        this.msg = msg;
        return this;
    }

}
